(function(ns, win, doc, cfgs){
ns.addPlay(1, {
    Extends: ns.Play,
    options: {
        length: 1,
		sum: 7,
		allRequired: false
    },
    build: function(ops, self) {
        self.element = new Element('div');
		self.balls = [new ns.Balls({title: '', min: 1, max: 35, mid: 18, isBile: true}), new ns.Balls({title:'', min: 1, max: 12, mid: 6, isBile:true, className: 'blue-ball'})];
		self.balls[0].addControl(new ns.RandomControl({min: 5, max: 16, name: '前区'}));
		self.balls[1].addControl(new ns.RandomControl({min: 2, max: 12, name: '后区'}));
        self.balls.invoke('inject', self.element);
    },
    compute: function (balls) {
        var self = this,
            ops = self.options,
			len0 = balls[0].length,
			len1 = balls[1].length,
			sum2 = 2,
			total = (len0 < ops.sum - sum2 ? 0 : len0.combination(ops.sum - sum2)) * (len1 < sum2 ? 0 : len1.combination(sum2)),
			info = {balls: balls.invoke('join', ',').join('|'), nums: len0, blues: len1, count: total, counts: [len0, len1]};
        return info;
    },
    change: function () {
        var self = this,
            balls = self.balls.invoke('get');
            self.info = self.compute(balls);
        self.parent();
    },
    get: function (n, tpl) {
        var self = this,
            ops = self.options,
            suffix = self.suffix,
			sum2 = 2,
            len,
            balls,
            nums,
            info,
            html;
        if (n) {
            html = [];
            len = self.balls.length;
            while (n -- > 0) {
				nums = [self.balls[0].random(ops.sum - sum2).join(','), self.balls[1].random(sum2).join(',')].join('|');
				html.push(tpl({cost: ops.cost, type: ops.type, name: ops.name, count: 1, nums: nums, balls: nums}));
            }
            return html.join('');
        } else {
            balls = self.balls.invoke('get');
            info = self.compute(balls);
            return info.count > 0 ? tpl({cost: ops.cost, type: ops.type, name: ops.name, count: info.count, nums: info.balls, balls: info.balls}) : '';            
        }
    }
});


ns.addPlay(3, {
    Extends: ns.CombPlay,
    options: {
		randomType: 1,
        length: 1,
		sum: 7
    },
    build: function(ops, self) {
        self.element = new Element('div');
		self.balls = [new ns.BileBalls({title: '前区', min: 1, max: 35, mid: 18, lower:1, upper: 4}), new ns.BileBalls({title: '后区', min: 1, max: 12, mid:6, lower: 0, upper: 1, className: 'blue-ball'}), new ns.BileBalls({title: '前区拖码', min: 1, max: 35, mid: 18, lower:0, upper: 16}), new ns.BileBalls({title: '后区拖码', min: 1, max: 12, mid:6, lower: 2, upper: 12, className: 'blue-ball'})];
        self.balls.invoke('inject', self.element);
    },
    compute: function (balls) {
        var self = this,
            ops = self.options,
			nums = balls.invoke('join'),
			sum = ops.sum,
			sum0 = 5,
			sum1 = sum - sum0,
            b0Len = balls[0].length,
            b1Len = balls[1].length,
			b2Len = balls[2].length,
			b3Len = balls[3].length,
            info = {nums: b0Len + b2Len, count: b0Len > 0 && b2Len >= sum0 - b0Len + 1 ? b2Len.combination(sum0 - b0Len) : 0};
		info.count *= b1Len > 0 && b3Len > 1 ? b3Len.combination(sum1 - b1Len) : 0;
		info.counts = [b0Len, b1Len, b2Len, b3Len];
		info.balls = ['前胆：', nums[0], '后胆：', nums[1], '前拖：', nums[2], '后拖：', nums[3]].join('&nbsp;');
        return info;
    },

	change: function (ball, force) {
        var self = this,
            balls = self.balls;
		if (ball === balls[0] || ball === balls[2]) balls[0].fix(ball, force, balls[2]);
		else if (ball === balls[1] || ball === balls[3]) balls[1].fix(ball, force, balls[3]);
        self.info = self.compute(balls.invoke('get'));
        self.fireEvent('change', self.info);
    },

    get: function (n, tpl) {
        var self = this,
            ops = self.options,
            suffix = self.suffix,
            len,
            balls,
            nums,
            info,
            html;
        if (n) {
            html = [];
            len = self.balls.length;
            while (n -- > 0) {
				nums = [self.balls[0].random(ops.sum - 1).join(','), self.balls[1].random(1)[0]].join('|');
                html.push(tpl({cost: ops.cost, type: ops.randomType, name: cfgs.plays[ops.randomType].name, count: 1, nums: nums, balls: nums}));
            }
            return html.join('');
        } else {
            if (!self.validate()) {
                App.Alert('胆码加拖码至于要有' + ops.sum + '个');
                return '';
            }
            if (self.balls.invoke('check').every(function(r){return r;})) {
                balls = self.balls.invoke('get');
                info = self.compute(balls);
                return tpl({cost: ops.cost, type: ops.type, name: ops.name, count: info.count, nums: balls.invoke('join', ',').join('|'), balls: info.balls});                          
            }          
        }
    }
});

new ns.Lottery(cfgs.lottery);
})(using('HB'), this, document, this.configs || {});
